﻿package UIComponents
{
	import Classes.GameUtils.MyTipSprite;
	import Classes.GameUtils.ToolTip_Sprite;
	import Classes.GameViews.ViewIcons.ViewIcon;
	
	import MyComponent.MyButton;
	
	import com.greensock.TweenLite;
	
	import fl.events.ComponentEvent;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.system.Capabilities;
	import flash.ui.Keyboard;
	
	public class UpOrDownWeaponPanel extends Sprite
	{
		public var qhBtn:MyButton;
		public var selectwqIcon:IconContainer;
		public var itemSp:DisplayObjectContainer;
		public var qhBtnBig:Sprite;
		
		public function UpOrDownWeaponPanel()
		{
			init();
		}
		
		private function init():void
		{
			this.selectwqIcon=this.getChildByName("_selectwqIcon") as IconContainer;
			this.qhBtn=this.getChildByName("_qhBtn") as MyButton;
			this.itemSp=this.getChildByName("TweenLiteContainer") as DisplayObjectContainer;
			this.qhBtnBig=this.getChildByName("_qhBtnBig") as Sprite;
			addLister();
		}
		
		private function addLister():void
		{
			this.qhBtn.addEventListener(ComponentEvent.BUTTON_DOWN,qhFun);
			this.qhBtnBig.addEventListener(MouseEvent.CLICK, qhFun);
			this.selectwqIcon.mouseEnabled	=	this.selectwqIcon.mouseChildren 	=	false;
			ToolTip_Sprite.getInstance().setTip( this.qhBtn, new MyTipSprite("按1-5可切换武器，全屏时不能用"), ToolTip_Sprite.DIRECTION_RIGHT);
		}
		//武器选取面板朝向  默认 向下
		public var flag:String="down";
		private function qhFun(e:Event):void//ComponentEvent):void
		{
			if(flag=="down")
			{
				flag="up"
				TweenLite.to(this.itemSp,0.5,{y:0});
			}else{
				flag="down"
				TweenLite.to(this.itemSp,0.5,{y:323});
			}
			this.dispatchEvent(new PanelEvent(PanelEvent.ADDARROW,true));
		}
		
		
		public function getWqFrame(index:int):SelectWeaponInnerPanel
		{
			if(index<0 || index>5)throw new Error("索引出错");
			return (this.itemSp.getChildByName("weaon"+(index+1))) as SelectWeaponInnerPanel;
		}
		
		
		/**
		 * 存储选好的武器 数组 
		 */		
		private var selectWeapons:Array;
		
		/**
		 * 提供外面赋值的方法  (装备武器)
		 * @param obj
		 */		
		public function setSelectWeaponObjData(obj:Object):void
		{
			this.selectWeapons=[];
			for each(var item:IUseItem in obj)
			{
				selectWeapons.push(item);
			}
			setSelectWeaponData();
			this.setSelectWq(this.selectWeapons[0] as IUseItem);
		}
		
		private function setSelectWeaponData():void
		{
			for(var i:int=0;i<5;i++)
			{
				var swp:SelectWeaponInnerPanel=this.getWqFrame(i);
				var swpItems:IUseItem=selectWeapons[i] as IUseItem;
				if(null!=swpItems)
				{
					swp.visible=true;
					swp.setData((i+1),swpItems,setSelectWq);
				}else{
					swp.visible=false;
				}
			}
		}
		public var currentWeaponId:String;
		public function setSelectWq(item:IUseItem):void
		{
//			trace("按钮了"+item.itemId);
			this.currentWeaponId=item.itemId.toString();
			var ss:DisplayObject=this.duplicateDisplayObject(item.itemPic,item,false);
			ss.scaleX=0.5;
			ss.scaleY=0.5;
			this.selectwqIcon.addPic(ss);//,item.itemName+"："+item.description);
			ToolTip_Sprite.getInstance().setTip(this.qhBtnBig,new MyTipSprite(item.itemName+"："+item.description),ToolTip_Sprite.DIRECTION_LEFT);
			
			this.dispatchEvent(new PanelEvent(PanelEvent.CHANGEUSERWEAPON,true));
//			trace("点中了ddd"+FlatInfo.newGuiderQHWq+"  "+item.itemId);
//			if(FlatInfo.newGuiderQHWq && item.itemId==11200002)
//			{
				flag="down"
				TweenLite.to(this.itemSp,0.5,{y:323});
//				trace("点中了");
//			}
//			this.updataShowNum(this.dfid,this.shdfSp,this.shdfSp2);
		}
		
		/**
		 * 图片克隆 
		 * @param target
		 * @param item
		 * @param autoAdd
		 * @return 
		 */		
		public function duplicateDisplayObject(target:DisplayObject,item:*, autoAdd:Boolean = false):DisplayObject {
			var targetClass:Class = Object(target).constructor;
			var duplicate:DisplayObject	= new targetClass(item) as DisplayObject;
			// duplicate properties
			duplicate.transform = target.transform;
			duplicate.filters = target.filters;
			duplicate.cacheAsBitmap = target.cacheAsBitmap;
			duplicate.opaqueBackground = target.opaqueBackground;
			if (target.scale9Grid) {
				var rect:Rectangle = target.scale9Grid;
				if (Capabilities.version.split(" ")[1] == "9,0,16,0"){
					// Flash 9 bug where returned scale9Grid as twips
					rect.x /= 20, rect.y /= 20, rect.width /= 20, rect.height /= 20;
				}
				duplicate.scale9Grid = rect;
			}
			// Flash 10 only
			// duplicate.graphics.copyFrom(target.graphics);
			// add to target parent's display list
			// if autoAdd was provided as true
			if (autoAdd && target.parent) {
				target.parent.addChild(duplicate);
			}
			return duplicate;
		}
		
		
		public function keyFun(e:KeyboardEvent):void
		{
			if(e.keyCode==97 || e.keyCode==49)
			{
				this.setSelectWq(this.selectWeapons[0] as IUseItem);
				return;
			}
			
			if(this.selectWeapons.length>=2 && (e.keyCode==98 || e.keyCode==50))
			{
				this.setSelectWq(this.selectWeapons[1] as IUseItem);
				return;
			}
			
			if(this.selectWeapons.length>=3 && (e.keyCode==99 || e.keyCode==51))
			{
				this.setSelectWq(this.selectWeapons[2] as IUseItem);
				return;
			}
			
			if(this.selectWeapons.length>=4 && (e.keyCode==100 || e.keyCode==52))
			{
				this.getWqFrame(3).dispatchEvent(new MouseEvent(MouseEvent.CLICK));
				return;
			}
			
			if(this.selectWeapons.length>=5 && (e.keyCode==101 || e.keyCode==53))
			{
				this.getWqFrame(4).dispatchEvent(new MouseEvent(MouseEvent.CLICK));
				return;
			}
		}
	}
}